package com.helloworld.upload.domain;

import java.util.ArrayList;
import java.util.List;

import com.cfw.exception.BizException;

public class ColumnInfo {
	
	private UploadInfo holder;
	private String targetColumn;
	private int sourceColumnIndex;
	private ValidateRule.TypeSize.EnumFieldType fieldType;
	private int size;
	private int scale;
	private boolean keyColumn;
	
	public void setUploadInfo(UploadInfo info) { this.holder = info; }
	
	public UploadInfo getUploadInfo() { return this.holder; }
	
	public String getTargetColumn() { return targetColumn; }
	
	public void setSourceColumnIndex(int sourceColumnIndex) { this.sourceColumnIndex = sourceColumnIndex; }
	
	public int getSourceColumnIndex() { return sourceColumnIndex; }
	
	public boolean isKeyColumn() { return keyColumn; }
	
	public ValidateRule.TypeSize.EnumFieldType getFieldType() { return this.fieldType; }
	
	public int getSize() { return this.size; }
	
	public int getScale() { return this.scale; }

	public List<ValidateRule> rules = new ArrayList<ValidateRule>();
	
	public void addValidateRule(ValidateRule rule) { this.rules.add(rule); }
	
		private static Object[][] ARGS_LEN_DEF = {
			{ValidateRule.EnumRule.TypeSize, 0},
			{ValidateRule.EnumRule.RI, 1},
			{ValidateRule.EnumRule.RiReplace, 1},
			{ValidateRule.EnumRule.Null, 0},
			{ValidateRule.EnumRule.Const, 1}
		};
		
		private static boolean validateArgsLength(ValidateRule.EnumRule enumRule, int length) {
			boolean isMatch = false;
			
			for (Object[] object : ARGS_LEN_DEF) {
				if (object[0] == enumRule) { 
					if ((Integer)object[1] == length) { isMatch = true; break; } 
				}
			}
			
			return isMatch;
		}
	
	public void addValidateRule(ValidateRule.EnumRule enumRule, Object ... args) {
		// args length 확인
		if (!validateArgsLength(enumRule, args.length)) { throw new BizException("E00001"); }
		
		ValidateRule rule = new ValidateRule();
		rule.setColumnInfo(this);
		
		switch (enumRule) {
		case TypeSize:
			rule.setRuleTypeSize();
			break;
		case RI:
			rule.setRuleRI((String)args[0]);
			break;
		case RiReplace:
			rule.setRuleRiReplace((String)args[0]);
			break;
		case Null:
			rule.setRuleNull();
			break;
		case Const:
			rule.setRuleConst((String)args[0]);
			break;
		default:
			throw new BizException("E00001");
		}
		
		this.rules.add(rule);
	}
	
	public List<ValidateRule> getValidateRules() { return rules; }

	public ColumnInfo(int sourceColumnIndex, String targetColumn, 
			ValidateRule.TypeSize.EnumFieldType fieldType, int size, int scale, boolean keyColumn) {
		this.sourceColumnIndex = sourceColumnIndex;
		this.targetColumn = targetColumn;
		this.fieldType = fieldType;
		this.size = size;
		this.scale = scale;
		this.keyColumn = keyColumn;
	}		
	
	@Override
	public String toString() {
		return new StringBuilder()
				.append("{targetColumn:").append(targetColumn)
				.append(", sourceColumnIndex:").append(sourceColumnIndex)
				.append(", fieldType:").append(fieldType)
				.append(", size:").append(size)
				.append(", scale:").append(scale)
				.append(", keyColumn:").append(keyColumn)
				.append("}").toString();
	}
}
